package com.qidianit.mes.pro.controller;

import com.github.pagehelper.PageInfo;
import com.qidianit.mes.pojo.Result;
import com.qidianit.mes.pojo.pro.Workflow;
import com.qidianit.mes.pro.service.WorkFlowService;
import com.qidianit.mes.pro.vo.WorkflowProcessByProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description:
 * @Author: shanzhe
 * @Date: 2025/9/18 - 13:56
 */
@RestController
@RequestMapping("/workflow")
public class WorkFlowController {
    @Autowired
    private WorkFlowService workFlowService;


    @GetMapping("/processWorkflowList")
    public Result<PageInfo<WorkflowProcessByProduct>> queryWorkflowProcessByProduct(@RequestParam(value = "id",required = false) Integer id,
                                                                                    @RequestParam("productionId") Integer productionId) {
        return Result.success(workFlowService.queryWorkflowProcessByProduct(id,productionId));
    }


    @GetMapping("/page")
    public Result queryWorkFlowListByManyTags(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                              @RequestParam(value = "size",defaultValue = "10") Integer size,
                                              @RequestParam(value = "workflowCode",required = false) String workflowCode,
                                              @RequestParam(value = "workflowName",required = false) String  workflowName,
                                              @RequestParam(value = "status",required = false) Integer status) {
        return Result.success(workFlowService.queryWorkFlowListByManyTags(page, size, workflowCode, workflowName, status));
    }

    @GetMapping("/code/{code}")
    public Result queryWorkFlowByCode(@PathVariable("code") String code) {
        Workflow workFlow = workFlowService.queryWorkFlowByCode(code);
        if (workFlow == null) {
            return Result.error("操作失败");
        }
        return Result.success(workFlow);
    }

    @PostMapping("/add")
    public Result addWorkFlow(@RequestBody Workflow workflow) {
        workflow.setCreateTime(LocalDateTime.now());
        workflow.setCreateBy("user11");
        if(workFlowService.addWorkFlow(workflow)==1){
        return Result.success(workflow);
        }else if (workFlowService.addWorkFlow(workflow)==-1){
            return Result.error("重复code");
        }
        return Result.error("操作失败");

    }

    @PutMapping("/update")
    public Result updateWorkFlow(@RequestBody Workflow workflow) {
        workflow.setUpdateTime(LocalDateTime.now());
        workflow.setUpdateBy("user11");
        if(workFlowService.updateWorkFlow(workflow)==1){
            return Result.success();
        }else if (workFlowService.updateWorkFlow(workflow)==-1){
            return Result.error("重复code");
        }
        return Result.error("操作失败");
    }

    @DeleteMapping("/delete/{id}")
    public Result deleteWorkFlow(@PathVariable("id") Integer id) {
      return   workFlowService.deleteWorkFlow(id) == 1?Result.success():Result.error("操作失败");

    }

    @DeleteMapping("/batchDelete")
    public Result batchDeleteWorkFlow(@RequestParam("id") List<Integer> id) {
        return workFlowService.deleteWorkFlowByListIds(id) == 1 ?Result.success("操作成功"):
                workFlowService.deleteWorkFlowByListIds(id) == -1?Result.error("成功了部分"):Result.error("操作失败");
    }
}
